		TITLE	INSTGSYM - Copyright (C) 1994 SLR Systems

		INCLUDE	MACROS

if	fg_cvpack
		INCLUDE	CVTYPES


		PUBLIC	INSTALL_GLOBALSYM


		.DATA

		EXTERNDEF	GSYM_HASH:DWORD,GSYM_HASH_LOG:DWORD,FIRST_GSYM_GINDEX:DWORD,LAST_GSYM_GINDEX:DWORD

		EXTERNDEF	CV_GSYM_GARRAY:STD_PTR_S


		.CODE	PASS2_TEXT

		externdef	_install_globalsym:proc
		externdef	_do_far_install:proc
		EXTERNDEF	_get_omf_name_length_routine:proc,_cv_gsym_pool_get:proc


INSTALL_GLOBALSYM	PROC
		push	ESI
		push	ECX
		push	EAX
		call	_install_globalsym
		add	ESP,12
		ret

		;
		;EAX IS HASH VALUE, CONVERT IT
		;ESI IS SYMBOL, ECX IS TEXT
		;SYMBOL IS CONSTANT, GDATA32, GDATA16, UDT
		;
		ASSUME	ESI:PTR CV_SYMBOL_STRUCT

		PUSHM	EDI,EBX

		XOR	EDX,EDX
		MOV	EDI,EAX
		
		div	dword ptr GSYM_HASH

		MOV	EBX,GSYM_HASH_LOG

		MOV	EAX,[EBX + EDX*4]
		LEA	EBX,[EBX + EDX*4 - CV_GLOBALSYM_STRUCT._NEXT_HASH_GINDEX]

		MOV	EDX,EDI
		JMP	FAR_CONT


		ASSUME	EBX:PTR CV_GLOBALSYM_STRUCT
FAR_NEXT1:
		POP	ECX
FAR_NEXT:
		MOV	EAX,[EBX]._NEXT_HASH_GINDEX
FAR_CONT:
		TEST	EAX,EAX
		JZ	DO_FAR_INSTALL

		ASSUME	EAX:PTR CV_GLOBALSYM_STRUCT

		MOV	EBX,EAX

		; Bugzilla http://d.puremagic.com/issues/show_bug.cgi?id=4009
		; The following dies with a segfault
		; No, EAX is not NULL, it's 6F7333231
		MOV	EDI,[EAX]._HASH

		CMP	EDI,EDX
		JNZ	FAR_NEXT
		;
		;PROBABLE MATCH, NEED COMPARE...
		;
		TEST	ECX,ECX
		JZ	FAR_TEXT_NUL

		MOV	EDI,[EBX]._TEXT_OFFSET
		PUSH	ECX

		TEST	EDI,EDI
		JZ	FAR_NEXT1

		PUSH	ESI
		MOV	ESI,ECX

		LEA	EDI,[EBX+EDI+ SIZE CV_GLOBALSYM_STRUCT-4]
		GET_OMF_NAME_LENGTH

		MOV	ECX,EAX

		push	ECX
		push	EDX
		push	EDI
		mov	EAX,ESP
		push	EAX
		call	_get_omf_name_length_routine
		add	ESP,4
		pop	EDI
		pop	EDX
		pop	ECX
;		CALL	GET_OMF_NAME_LENGTH_EDI

		CMP	EAX,ECX
		JNZ	FAR_TEXT_MATCH

		REPE	CMPSB
FAR_TEXT_MATCH:
		POP	ESI
		JNZ	FAR_NEXT1
		;
		;COMPARE SYMBOL TYPE (NOT LENGTH, AS DWORD ALIGNMENT MAY OR MAY NOT BE DONE)
		;
		MOV	AL,BPTR [ESI]._ID
		MOV	CL,BPTR [EBX]._ID

		CMP	AL,CL
		JNZ	FAR_MATCH_FAIL

		MOV	AL,BPTR [ESI]._ID+1
		MOV	CL,BPTR [EBX]._ID+1

		CMP	AL,CL
		JNZ	FAR_MATCH_FAIL
FAR_MATCH_DELETE:
		MOV	[ESI]._ID,I_S_DELETE
FAR_MATCH_FAIL:
		;
		;FAIL MEANS ANOTHER SYMBOL SAME NAME, DIFFERENT KIND, DON'T
		;DELETE THIS ONE
		;
		POPM	ECX,EBX,EDI

		RET

FAR_TEXT_NUL:
		ASSUME	ESI:PTR CV_IREF_STRUCT,EBX:PTR CVG_REF_STRUCT
		;
		;NO NAME, ONE MUST BE A REFERENCE
		;
		;
		;TEST SEGMENT AND OFFSET FOR KICKS
		;
		MOV	EAX,[ESI]._OFFSET
		MOV	EDI,[EBX]._OFFSET

		CMP	EDI,EAX
		JNZ	FAR_NEXT

		MOV	EAX,DPTR [ESI]._LENGTH
		MOV	EDI,DPTR [EBX]._LENGTH

		AND	EAX,0FFFF0000H			;PRESERVE ID ONLY
		AND	EDI,0FFFF0000H

		CMP	EAX,EDI				;TRY AGAIN IF ONLY ONE IS A REFERENCE
		JNZ	FAR_NEXT

		MOV	EAX,DPTR [ESI]._MODULE
		MOV	EDI,DPTR [EBX]._MODULE

		AND	EAX,0FFFF0000H			;PRESERVE SEGMENT ONLY
		AND	EDI,0FFFF0000H

		CMP	EAX,EDI				;TRY AGAIN IF SEGMENTS DON'T MATCH
		JNZ	FAR_NEXT

		MOV	[ESI]._ID,I_S_DELETE

		POPM	EBX,EDI

		RET

INSTALL_GLOBALSYM	ENDP


DO_FAR_INSTALL	PROC	PRIVATE
		push	ESI
		push	EDX
		push	ECX
		push	EBX
		call	_do_far_install
		add	ESP,16
		pop	EBX
		pop	EDI
		ret

		;
		;EBX GETS POINTER...
		;
		MOV	EAX,DPTR [ESI]._LENGTH

		AND	EAX,0FFFFH

		ADD	EAX,SIZE CV_GLOBALSYM_STRUCT-2

		push	ECX
		push	EDX
		push	EAX
		call	_cv_gsym_pool_get
		add	ESP,4
		pop	EDX
		pop	ECX

		MOV	EDI,EAX
		ASSUME	EDI:PTR CVG_REF_STRUCT

		MOV	[EBX]._NEXT_HASH_GINDEX,EAX
		MOV	EBX,LAST_GSYM_GINDEX

		MOV	[EDI]._HASH,EDX
		MOV	LAST_GSYM_GINDEX,EAX

		TEST	EBX,EBX
		JZ	L3$

		MOV	[EBX]._NEXT_GSYM_GINDEX,EAX
L4$:
		TEST	ECX,ECX			;IS THERE TEXT?

		MOV	EAX,ECX
		JZ	L5$

		SUB	EAX,ESI			;OFFSET TO TEXT
L5$:
		XOR	EDX,EDX

		MOV	[EDI]._TEXT_OFFSET,EAX
		MOV	[EDI]._NEXT_HASH_GINDEX,EDX

		MOV	ECX,DPTR [ESI]._LENGTH
		MOV	[EDI]._NEXT_GSYM_GINDEX,EDX

		ADD	ECX,2
		MOV	EAX,ESI

		AND	ECX,0FFFFH
		LEA	EDI,[EDI]._LENGTH

		cmp	ECX,8
		jb	L123
		push	ECX
		shr	ECX,2
		rep	movsd
		pop	ECX
		and	ECX,3
		je	L125
L123:
		rep	movsb

L125:		MOV	ESI,EAX
		POP	EBX

		MOV	[ESI]._ID,I_S_DELETE
		POP	EDI

		RET

L3$:
		MOV	FIRST_GSYM_GINDEX,EAX
		JMP	L4$

DO_FAR_INSTALL	ENDP

endif

		END

